import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    name: 'homeview',
    redirect: '/reportsa',
    component: () => import('../views/homeview.vue'),
    children: [
      {
        path: '/reportsa',
        name: 'reportsa',
        component: () => import('../views/reportsa.vue')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/user/users.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/Permission/rights.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/Permission/roles.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/orders/orders.vue')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../views/good/categories.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../views/good/goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/good/params.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/echtras/reports.vue')
      },
      {
        path: '/dialog',
        name: 'dialog',
        component: () => import('../views/echtras/dialog.vue')
      },{
        path: '/addGoods',
        name: 'addgoods',
        component: () => import('../views/good/addGoods.vue'),
      }

    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  // console.log(token)
  const isLogin = (to.path === '/login')
  // 如果有token，并且是在登录页，那么跳转到首页
  if (token && isLogin) {
    return next({ name: 'homeview' })
  }
  // 如果没有token，并不是在登录页，跳转到登录页
  if (!token && !isLogin) {
    return next({ name: 'login' })
  }
  next()
})
export default router
